package com.example.liuke.guangguang.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.example.liuke.guangguang.R;
import com.example.liuke.guangguang.videopicwebview.Getfiles;
import com.example.liuke.guangguang.videopicwebview.WebVideoSliderView;
import com.example.liuke.guangguang.videopicwebview.WebVideoView;

import java.util.ArrayList;
import java.util.HashMap;

public class VideopicActivity extends AppCompatActivity {
    private SliderLayout sliderLayout;
    long currentTime;
    int currentPosition = -1;
    View currentSlider, view1;
    private ArrayList<String> videourl_list;
    private WebVideoView webVideoView;
    private HashMap<Integer, BaseSliderView> myhasmap = new HashMap<Integer, BaseSliderView>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videopic);
        sliderLayout = (SliderLayout) findViewById(R.id.myslid);
        initAd();
    }

    //webview的 onResume方法可以继续播放，onPause可以暂停播放
    @Override
    protected void onResume() {
        super.onResume();
        if (currentSlider != null) {
            WebVideoView webVideoView = (WebVideoView) currentSlider.findViewById(R.id.web_video_view);
            if (webVideoView != null) {
                webVideoView.playVideo();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (currentSlider != null) {
            WebVideoView webVideoView = (WebVideoView) currentSlider.findViewById(R.id.web_video_view);
            if (webVideoView != null) {
                webVideoView.pauseVideo();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sliderLayout.stopAutoCycle();
    }

    private void initAd() {
        videourl_list = Getfiles.getImagePathFromSD("bankvideo");
        //获取屏幕参数
        DisplayMetrics dMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dMetrics);
        int sliderLayoutWidth = dMetrics.widthPixels;
        //int sliderLayoutHeight = sliderLayoutWidth * 9 / 21;
        int sliderLayoutHeight = dMetrics.heightPixels;
        RelativeLayout.LayoutParams itemViewParams = new RelativeLayout.LayoutParams(sliderLayoutWidth, sliderLayoutHeight);
        sliderLayout.setLayoutParams(itemViewParams);
        sliderLayout.removeAllSliders();
        //sliderLayout.setDuration(6000);
        sliderLayout.setPresetTransformer(SliderLayout.Transformer.Accordion);
        sliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);//设置指示器的位置
        /*sliderLayout.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d("posp",position+"");
                String string = "";
                string = videourl_list.get(position);
                if (string.endsWith(".mp4")) {
                    View v = LayoutInflater.from(VideopicActivity.this).inflate(R.layout.video, null);
                    VideoView videoView = (VideoView)v.findViewById(R.id.myvideo);
                    //VideoView videoView = (VideoView)myhasmap.get(position).getView().findViewById(R.id.myvideo);
                    //videoView.pause();
                    sliderLayout.stopAutoCycle();
                    //sliderLayout.startAutoCycle(Getvideotime.getvideotime(string),3000,true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });*/
        //sliderLayout.setCustomAnimation(new DescriptionAnimation());
        sliderLayout.setCustomAnimation(new DescriptionAnimation() {
            /*@Override
            public void onPrepareCurrentItemLeaveScreen(View currentview) {
                Log.d("onPrepareCurrent", "1");
                currentPosition = sliderLayout.getCurrentPosition();
                currentSlider = currentview;
                if (currentPosition != -1) {//??干吗用的
                    String string = "";
                    string = videourl_list.get(currentPosition);
                    if (string.endsWith(".mp4")) {
                        WebVideoView webVideoView = (WebVideoView) currentSlider.findViewById(R.id.web_video_view);
                        if (webVideoView != null) {
                            Toast.makeText(VideopicActivity.this, "关闭了" + string, Toast.LENGTH_LONG).show();
                            webVideoView.pauseVideo();
                        }
                        sliderLayout.startAutoCycle(3000, 3000, true);
                    }
                }
            }*/



            @Override
            public void onNextItemAppear(View view) {
                Log.d("onNextItemAppear", "2");
                view1 = view;
                String string = "";
                if (currentPosition != -1) {
                    string = videourl_list.get(currentPosition);
                    if (string.endsWith(".mp4")) {
                        WebVideoView webVideoView = (WebVideoView) currentSlider.findViewById(R.id.web_video_view);
                        if (webVideoView != null) {
                            Toast.makeText(VideopicActivity.this, "关闭了" + string, Toast.LENGTH_LONG).show();
                            webVideoView.pauseVideo();
                        }
                        sliderLayout.startAutoCycle(3000, 3000, true);
                    }
                }
                currentPosition = sliderLayout.getCurrentPosition();
                currentSlider = view;
                string = videourl_list.get(currentPosition);
                if (string.endsWith(".mp4")) {
                    sliderLayout.stopAutoCycle();
                    webVideoView = (WebVideoView) view1.findViewById(R.id.web_video_view);
                    if (webVideoView != null) {
                        Toast.makeText(VideopicActivity.this, "bofang" + string, Toast.LENGTH_LONG).show();
                        webVideoView.setWebVideoEventListener(new WebVideoView.WebVideoEventListener() {
                            @Override
                            public void onPlaying() {
                                Toast.makeText(VideopicActivity.this, "bofangzhong", Toast.LENGTH_LONG).show();
                                view1.findViewById(R.id.progress).setVisibility(View.INVISIBLE);
                            }

                            @Override
                            public void onEnded() {
                                webVideoView.pauseVideo();
                                sliderLayout.startAutoCycle(0, 3000, true);
                            }

                            @Override
                            public void onError() {
                                webVideoView.pauseVideo();
                                sliderLayout.startAutoCycle(0, 3000, true);
                            }
                        });
                        webVideoView.playVideo();
                    }
                }
            }
        });
        for (int i = 0; i < videourl_list.size(); i++) {
            if (videourl_list.get(i).endsWith(".mp4")) {
                WebVideoSliderView webVideoSliderView = new WebVideoSliderView(this);
                webVideoSliderView.loadUrl("file://" + videourl_list.get(i));//视频的url
                sliderLayout.addSlider(webVideoSliderView);
                myhasmap.put(i, webVideoSliderView);
            } else {
                TextSliderView textSliderView = new TextSliderView(this);
                textSliderView.image("file://" + videourl_list.get(i)).setScaleType(BaseSliderView.ScaleType.Fit);//图片的url
                sliderLayout.addSlider(textSliderView);
                myhasmap.put(i, textSliderView);
            }
        }
       /* for (String string: videourl_list) {
            if (string.endsWith(".mp4")) {
                WebVideoSliderView webVideoSliderView = new WebVideoSliderView(this);
                webVideoSliderView.loadUrl("file://"+string);//视频的url
                sliderLayout.addSlider(webVideoSliderView);
            } else {
                TextSliderView textSliderView = new TextSliderView(this);
                textSliderView.image("file://"+string).setScaleType(BaseSliderView.ScaleType.Fit);//图片的url
                sliderLayout.addSlider(textSliderView);
            }
        }*/

                /*String stringa = "";
                stringa = videourl_list.get(0);
                if (string.endsWith(".mp4")) {
                    sliderLayout.startAutoCycle(Getvideotime.getvideotime(stringa), 6000, true);
                } else {
                    sliderLayout.startAutoCycle(5000, 6000, true);
                }*/
        sliderLayout.startAutoCycle(3000, 3000, true);
    }
}
